package com.navinfo.opentsp.qingqi.trackoptimize.service;

import com.mongodb.*;
import com.navinfo.opentsp.qingqi.trackoptimize.entity.MongoTripDataEntity;
import com.navinfo.opentsp.qingqi.trackoptimize.util.DateUtil;
import com.navinfo.opentsp.qingqi.trackoptimize.util.HttpUtil;
import com.navinfo.opentsp.qingqi.trackoptimize.util.JsonUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author wangshuai
 * @date 2019/4/4 14:37
 */
public class Test {
    public static void main(String[] args) {
        try {
            List<String> list = getFromMongo("10.30.50.153",27020,"wangshuai_test","car_trip_info_20190401","14807421400");
            for(int i=0;i<list.size();i++) {
            MongoTripDataEntity trip = JsonUtil.fromJson(list.get(i), MongoTripDataEntity.class);
                String ksjson = HttpUtil.get("http://api.map.baidu.com/geocoder/v2/?location=" + trip.get_start_lat() + "," + trip.get_start_lon() + "&output=json&pois=1&ak=tjqG8Gub8I6reuc5eZLQU639xqaYd7jv", "", "");
                String jsjson = HttpUtil.get("http://api.map.baidu.com/geocoder/v2/?location=" + trip.get_end_lat() + "," + trip.get_end_lon() + "&output=json&pois=1&ak=tjqG8Gub8I6reuc5eZLQU639xqaYd7jv", "", "");
                System.out.println("车辆的通信号：" + trip.get_auto_terminal());
                System.out.println("行程开始时间：" + DateUtil.formatTime(new Date(trip.get_start_time())));
                System.out.println("行程结束时间：" + DateUtil.formatTime(new Date(trip.get_end_time())));
                Map<String, Object> map1 = JsonUtil.toMap(ksjson);
                Map<String, Object> map2 = JsonUtil.toMap(jsjson);
                System.out.println("行程起点    ：" + ((Map) map1.get("result")).get("formatted_address") + "  " + ((Map) map1.get("result")).get("sematic_description") + "   ");
                System.out.println("行程终点    ：" + ((Map) map2.get("result")).get("formatted_address") + "  " + ((Map) map2.get("result")).get("sematic_description") + "   ");
                System.out.println("引擎工作时长：" + (trip.get_trip_time()/1000) + "秒（约" + trip.get_trip_time() / 60000 + "分钟）");
                System.out.println("行程内里程  ：" + trip.get_trip_len() + "公里");
                System.out.println("行程内油耗  ：" + trip.get_total_useoil() + "升");
                System.out.println("平均油耗    ：" + trip.get_avghundoil()+ "升/百公里");
                System.out.println("最大车速    ：" + trip.get_max_speed() + "Km/h");
                System.out.println("平均速度    ：" + trip.get_avg_not_idling_speed() + "Km/h（不含怠速）");
                System.out.println("平均速度    ：" + trip.get_avg_speed() + "Km/h（含怠速）");
                System.out.println("行程得分    ：" + trip.get_trip_score() + "分");
                System.out.println("驾驶得分    ：" + trip.get_driv_score() + "分");
                System.out.println("油耗得分    ：" + trip.get_oil_score() + "分");
                System.out.println("怠速时长    ：" + trip.get_zeroup_time() / 1000.0 + "秒");
                System.out.println("0~30迈时长  ：" + trip.get_slowspeed_time() / 1000.0 + "秒");
                System.out.println("30~60迈时长 ：" + trip.get_lowspeed_time() / 1000.0+ "秒");
                System.out.println("60~80迈时长 ：" + trip.get_midspeed_time() / 1000.0 + "秒");
                System.out.println("80~100迈时长：" + trip.get_highspeed_time() / 1000.0 + "秒");
                System.out.println("100以上时长 ：" + trip.get_overspeed_time() / 1000.0 + "秒");
                System.out.println("=========================");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static List<String> getFromMongo(String ip, int port, String dbname, String coll,String tid) {
        List<String> strings = new ArrayList<>();
        String json = "";
        // 实例化Mongo对象，连接27017端口
        Mongo mongo = new Mongo(ip, port);
        // 连接名为yourdb的数据库，假如数据库不存在的话，mongodb会自动建立
        DB db = mongo.getDB(dbname);
        // 从Mongodb中获得名为yourColleection的数据集合，如果该数据集合不存在，Mongodb会为其新建立
        DBCollection collection = db.getCollection(coll);
        // 创建要查询的document
        BasicDBObject searchQuery = new BasicDBObject();
        searchQuery.put("_auto_terminal", tid);

//        searchQuery.put("routeStartTime", starttime);
        // 使用collection的find方法查找document
        DBCursor cursor = collection.find(searchQuery).sort(new BasicDBObject().append("_start_time",1));
        // 循环输出结果
        while (cursor.hasNext()) {
            DBObject object = cursor.next();
            try {
                json = JsonUtil.toJson(object);
                String[] b = json.split(",\"altitudeRanges\":");
                json = b[0] + "}";
                strings.add(json);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        cursor.close();
        return strings;
    }
}
